<?php
namespace Admin\Controller;

use Think\Controller;

class MainController extends Controller{
    /*
     * 显示后台主页面
     * */
    public function main(){
        $name = $_SESSION['ad_user'];
        $this->assign('name',$name);
        $this->display('main');
    }
    
    /*
     * 显示后台主页面
     * */
    public function index(){
        $this->display();
    }

    /*
     * 显示企业信息页面
     * */
    public function get_info(){
        //获取用户模型对象
        $User   = D('company');
        //数据分页
        $page   = isset($_GET['page']) ? $_GET['page'] : 1;
        $count  = $User->count();
        $number = (int)($count/3)+1;
        $this->assign('number', $number);
        $data = $User->limit(($page-1)*3,3)->select();
        //数据给模板赋值
        $this->assign('page', $page);
        $this->assign('cp_data',$data);
        //显示页面
        $this->display();
    }

    /**
     * 查看企业详细信息
     */
    public function look(){
        $id = $_GET['id'];
        $company = D('company');
        $data = $company->where(['cp_id'=> $id])->select();
        //数据给模板赋值
        $this->assign('cp_data',$data);
        //显示页面
        $this->display();
    }

    /**
     * 修改企业信息
     */
    public function update(){
        $id = $_GET['id'];
        $company = D('company');
        $data = $company->where(['cp_id'=>$id])->select();
        //数据给模板赋值
        $this->assign('cp_data',$data);
        //显示页面
        $this->display();
    }

    /**
     * 给数据库插入数据
     */
    public function data_update(){
        $id = $_GET['id'];
        $data['cp_name']   = I('post.cp_name');
        $data['cp_place']  = I('post.cp_place');
        $data['cp_status'] = I('post.cp_status');
        $company = D('company');
        $company->where(['cp_id'=>$id])->save($data);
        $this->success('更改成功', '/main/get_info');
        $this->display('aaa');
    }

    /**
     * 增加企业显示页面
     */
    public function  insert_info()
    {
    // 接收企业信息
        if(IS_POST){
         //获取模型的对
            $company = D('company');
            //接收数据
            $data['cp_name']   = I('post.cp_name');
            $data['password']  = I('post.password');
            $data['cp_user']   = I('post.cp_user');
            $data['cp_place']  = I('post.cp_place');
            $data['cp_time']   = date('Y-m-d,h:s:m');
            $data['admin_id']  = $_SESSION['id'];

         //对所输入的数据进行插入到数据库中
            $company->data($data)->add();
         //返回
            $this->success('添加成功','get_info');
        } else
        {
            $this->display('insert_info');
        }
    }
    
     /**
     * 删除一条企业记录
     */
    public function del_info()
    {
        $User = D('company');
        $id   = $_GET['id'];
        $User->where(['cp_id'=>$id])->delete();
        $this->success('删除成功！','/main/get_info');
        //      $this->redirect('/main/get_info');
    }
    
    /**
     * 查找一条企业的信息
     */
    public  function select_info()
    {
        $id = $_SESSION['id'];
        $User = D('company');
        $User->where(['id'=>$id])->select();
        $this->display('/main/look');
    }

}
